// label.js
var app = getApp()
var host = app.globalData.host
var img = app.globalData.image
Page({
  data: {
    page : 1,
    total:'',
    haspage: false,
    length:'',
    id:'',
    labelarr:[],
    labelcontent:[],
    shownone:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    that.setData({
      name: options.name,
      img:img,
      id:id
    })
    var name = options.name;
    console.log(that.data.page);
    wx.request({
      url: host + '/spuApi/spuTagShow?tag='+ id +'&pageNo=' + that.data.page,
      header: { 'content-type': 'application/json' },
      success:function(res){
        that.setData({
          labelcontent: res.data.data.listSpu,
          labelarr: res.data.data.listSpu,
          length: res.data.data.listSpu.length,
          page: that.data.page + 1,
          total:res.data.data.count
        })
        console.log(that.data.page);
        console.log(that.data.labelarr);
        if (that.data.length < 10) {
          that.setData({
            haspage: false
          })
        } else {
          that.setData({
            haspage: true
          })
        }
      }      
    }) 
  },
  onReachBottom:function(){
    console.log(this.data.labelarr)
    console.log(this.data.labelcontent)
    
    var that = this;
    console.log(that.data.haspage);
    if (that.data.haspage){
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: host + '/spuApi/spuTagShow?tag=' + that.data.id + '&pageNo=' + that.data.page,
        header: { 'content-type': 'application/json' },
        success:function(res){
          console.log(res.data.data)
          that.setData({
            labelcontent: res.data.data.listSpu,
            length: res.data.data.listSpu.length
          })
          console.log(that.data.labelcontent)
          if (that.data.length == 10){
            that.setData({
              page: that.data.page + 1,
              labelarr: that.data.labelarr.concat(that.data.labelcontent),
              shownone: false
            })
          } else if (0 < that.data.length && that.data.length < 10){
            that.setData({
              page: that.data.page + 1,
              labelarr: that.data.labelarr.concat(that.data.labelcontent),
              haspage:false,
              shownone: true
            })
          }else{
            that.setData({
              haspage: false,
              shownone: true
            })
          }
          setTimeout(function () {
            wx.hideLoading()
          }, 500)
        }
      })
    }
  },
  // 跳转至商品详情
  toweddetail:function(event){
    var wedid = event.currentTarget.dataset.id;
    wx.redirectTo({
      url: '../wedproductdetail/wedproductdetail?id=' + wedid,
    })
  }
})